import projectService from '@/api/projectService'
import { loading } from '@/api/pageInitFn'
import projectBaseService from '@/api/projectBaseService'

export default function (owner) {
  this.formatData = (list) => {
    const rs = []
    if (!list || !list.length) {
      return []
    } else {
      list.forEach(business => {
        const bus = {
          name: business.BUSINESSNAME,
          checkable: false,
          selectable: false,
          key: business.ID,
          icon: business.ICON,
          children: []
        }
        if (business.children && business.children.length > 0) {
          business.children.forEach(process => {
            bus.children.push({
              name: process.NAME,
              businessId: process.BUSINESSID,
              processAppId: process.PROCESSAPPID,
              businessProcessId: process.REALID,
              checkable: true,
              selectable: true,
              key: process.ID
            })
          })
          rs.push(bus)
        }
      })
    }
    return rs
  }
  this.openHandlePage = (projectId, query) => {
    const { href } = owner.$router.resolve({
      name: owner.pageDetailRoute.routerName,
      params: { id: projectId },
      query
    })
    window.open(href, '_blank')
  }
  this.openXzzxHandlePage = (projectId, taskId) => {
    const query = {
      activityName: '受理',
      correctId: null,
      projectId: projectId,
      taskId: taskId
    }
    const { href } = owner.$router.resolve({
      name: '402886cb8479f1fd0184989eced0059e',
      params: { id: projectId },
      query
    })
    window.open(href, '_blank')
  }
  this.doCreateForestProject = (process, initData) => {
    const itemIdList = []
    const startPrjParamDTO = {
      businessId: process.businessId,
      businessProcessId: process.businessProcessId,
      createType: 0,
      dataCopyType: 0,
      dataOverride: true,
      initMaterial: true,
      processAppId: process.processAppId,
      processId: process.processId,
      useProcessArgs: true,
      // initCaseCode: true,
      // initProjectCode: true,
      // caseCodeField: CASE_NUMBER_PROJECT_BASE,
      // projectCodeField: ITEM_NUMBER_PROJECT_BASE,
      itemIdList: itemIdList,
      initData: initData || {}
    }
    projectService.startProject(startPrjParamDTO).then(({ projectId, taskId }) => {
      this.openHandlePage(projectId, { taskId: taskId })
    })
  }

  this.createYtgzProject = (process, initData, createDialInst, hasLoading = true, callBack = null) => {
    let closeLoading = null
    if (hasLoading) {
      closeLoading = loading('正在操作请稍候！')
    }

    var initProjectCode = true
    var projectCode = null
    if (initData.projectId) {
      // initData = {}
      if (initData.initProjectCode) {
        initProjectCode = false
        projectCode = initData.initProjectCode
      }
    }
    if (initData.initProjectCode) {
      initProjectCode = !initData.initProjectCode
      delete initData.initProjectCode
    }
    let sllx = null
    if (process.name.indexOf('县级') >= 0) {
      sllx = '县级'
    }
    if (process.name.indexOf('市级') >= 0) {
      sllx = '市级'
    }
    if (process.name.indexOf('省级') >= 0) {
      sllx = '省级'
    }
    if (!initData.bz_projectcase) {
      initData.bz_projectcase = []
    }
    if (initData.bz_projectcase <= 0) {
      initData.bz_projectcase.push({})
    }
    initData.bz_projectcase[0].sllx = sllx
    const startPrjParamDTO = {
      businessId: process.businessId,
      businessProcessId: process.businessProcessId,
      createType: initData.doAgain ? 2 : initData.projectId ? 1 : 0,
      dataCopyType: initData.doAgain ? 2 : initData.projectId ? 4 : 0,
      dataOverride: true,
      initMaterial: true,
      parentProjectId: initData.projectId ? initData.projectId : undefined,
      processAppId: process.processAppId,
      processId: process.processId,
      useProcessArgs: true,
      initCaseCode: true,
      initProjectCode: initProjectCode,
      caseCodeField: '案卷基本信息.案卷编号',
      projectCodeField: '案卷基本信息.项目编号',
      // initData: initData.projectId ? { } : initData
      initData: initData.doAgain ? initData.INIT_DATA : initData.projectId ? {
        BZ_PROJECTCASE: [{
          projectcode: projectCode
        }]
      } : initData
    }
    projectService.startYtgzProject(startPrjParamDTO).then(({ projectId, taskId }) => {
      createProjectBase(projectId)
      projectService.updateProjectDueTime(projectId, initData.bz_zsxx ? initData.bz_zsxx[0].nbgzr : 1)

      if (callBack) {
        callBack(projectId)
      }
      owner.Event.$emit('_REFRESH_CFM_BOX_')
      if (hasLoading) {
        closeLoading()
      }
      if (process.businessId == '2c92b0e688b4f91b0188b771bdd50003') {
        // 新建许可注销业务特殊处理
        this.openXzzxHandlePage(projectId, taskId)
      } else {
        this.openHandlePage(projectId, { taskId: taskId })
      }
      if (createDialInst) {
        createDialInst.$destroy()
      }
    })
  }

  this.startComposeProject = (process, projectIds) => {
    const closeLoading = loading('正在操作请稍候！')
    const startPrjParamDTO = {
      businessId: process.businessId,
      businessProcessId: process.businessProcessId,
      createType: 0,
      dataCopyType: 0,
      dataOverride: true,
      initMaterial: true,
      processAppId: process.processAppId,
      processId: process.processId,
      useProcessArgs: true,
      initCaseCode: true,
      initProjectCode: true,
      caseCodeField: '案卷基本信息.案卷编号',
      projectCodeField: '案卷基本信息.项目编号',
      initData: {}
    }
    projectService.startComposeProject({ projectIds: projectIds }, startPrjParamDTO).then(({ projectId, taskId }) => {
      closeLoading()
      this.openHandlePage(projectId, { taskId: taskId })
      owner.Event.$emit('_REFRESH_TODO_BOX_')
    })
  }
  /**
   * 通过projecId创建大项目
   * @param projectId
   */
  function createProjectBase (projectId, stageType, broadcategory) {
    const params = {
      projectId: projectId,
      xmtydm: null
    }
    projectBaseService.createProjectBaseByProjectId(params).then(
      (d) => {
        const base = d
        projectService.getBusinessByProjectId(projectId).then((d) => {
          base.objectId = projectId
          base.stageType = 'GHXK'
          base.broadcategory = d.broadCategory
          projectBaseService.saveProjectBase(base).then(({ data }) => {
            console.log(data)
          })
        })
      }
    )
  }
}
